/*
    编辑商品:商品类型
 */
$(function () {
   getGrid();
});

function getGrid() {
    var colunms = TreeGrid.initColumn();
    var table = new TreeTable(TreeGrid.id, '../category/queryAll', colunms);
    table.setExpandColumn(2);
    table.setIdField("id");
    table.setCodeField("id");
    table.setParentCodeField("parentId");
    table.setExpandAll(false);
    table.setHeight($(window).height() - 100);
    table.init();
    TreeGrid.table = table;
};
var TreeGrid = {
    id: "jqGrid",//表格绑定的标签ID
    table: null,
    layerIndex: -1
};
TreeGrid.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '分类编号', field: 'id', visitable: false, align: 'center', valign: 'middle', width: '40px',key: true,hidden: true},
        {title: '分类名称', field: 'name', align: 'center', valign: 'middle', width: '80px',align:'center'},
        {title: '排序值', field: 'sortOrder', align: 'center', valign: 'middle', width: '80px',align:'center'},
        {title: '分类图片', field: 'imageUrl', align: 'center', valign: 'middle', width: '80px',align:'center',
            formatter: function (value) {
                if (value != null) {
                    return '<img width="50px" height="50px" src="' + value.imageUrl + '">';
                }
                return '';
            }
        },
        {title: '描述', field: 'frontDesc', align: 'center', valign: 'middle', width: '80px',align:'center'},
        {title: '服务费', field: 'rate', align: 'center', valign: 'middle', width: '80px',align:'center'},
        {title: '赠送能量利率', field: 'energyRate', align: 'energyRate', valign: 'middle', width: '80px',align:'center'},
        {title: '释放能量费率', field: 'spcFree', align: 'spcFree', valign: 'middle', width: '80px',align:'center'},
        {title: '团队赠送', field: 'gaveTeam', align: 'gaveTeam', valign: 'middle', width: '80px',align:'center'},
        {title: '是否显示', field: 'show', align: 'center', valign: 'middle', width: '80px',align:'center',
            formatter: function (item) {
                if (item.show === 0) {
                    return '<span class="label label-primary">不显示</span>';
                }
                if (item.show === 1) {
                    return '<span class="label label-success">显示</span>';
                }
            }
        },
        {title: '创建时间', field: 'createDate', align: 'center', valign: 'middle', width: '80px',align:'center'}
    ];
    return columns;
};

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        categorysL1:[],
        proCategory: {},
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.getCategorys();
            vm.proCategory = {};
        },
        update: function (event) {
            var menuId = TreeGrid.table.getSelectedRow();
            if (menuId.length == 0) {
                iview.Message.error("请选择一条记录");
                return;
            }
            var id = menuId[0].id;
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id);
            vm.getCategorys();
        },
        saveOrUpdate: function (event) {
            let url = vm.proCategory.id == null ? "../category/save" : "../category/update";
            if (vm.proCategory.parentId == null || vm.proCategory.parentId === ''){
                iview.Message.error("请选择上级分类");
                return;
            }
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.proCategory),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var menuId = TreeGrid.table.getSelectedRow();
            if (menuId.length == 0) {
                iview.Message.error("请选择一条记录");
                return;
            }
            var id = menuId[0].id
            confirm('确定要删除选中的记录？', function () {

                Ajax.request({
                    url: "../category/delete?id="+id,
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../category/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.proCategory = r.tProCategory;
                }
            });
        },
        getCategorys: function () {
            vm.categorysL1 = [];
            Ajax.request({
                url: "../category/queryAll?parentId=0",
                async: true,
                successCallback: function (r) {
                    vm.categorysL1 = r.listL1;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            getGrid();
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        handleSuccessImageUrl: function (res, file) {
            vm.proCategory.imageUrl = file.response.url;
        },
        eyeImageUrl: function () {
            var url = vm.proCategory.imageUrl;
            eyeImage(url);
        },
        eyeImage: function (e) {
            eyeImage($(e.target).attr('src'));
        }
    }
});
